<?php
/**
 *
 * 版权所有：安德玛(underArmour)
 * 作    者：1554324405@qq.com
 * 日    期：2020-05-20
 * 版    本：1.0.0
 * 功能说明：用户等级控制器。
 *
 **/
namespace Api\Controller;

class UserLevelController extends ComController
{
    /**
     * 用户等级列表
     */
    public function index()
    {
        $list = M('user_level')->order('o asc')->select();
        foreach ($list as $k=>&$v){
            $list[$k]['icon'] = C('URL').$v['icon'];
            $list[$k]['card_pic'] = C('URL').$v['card_pic'];
            $list[$k]['price'] = round($v['price'],2);
        }
        $this->res['msg'] = 'success';
        $this->res['data'] = $list;
        $level_id = $this->user['level_id'];
        if($level_id == 1){
            $level_id = 3;
        }
        $this->res['user_level_id'] = strval($level_id);
        $this->response($this->res,'json');
    }
    
    /**
     * 用户提交申请会员
     */
    public function confirm()
    {
        $id = I('get.id','','htmlentities');
        if(!$id){
            $this->res['code'] = 101;
            $this->res['msg'] = '请传入会员等级id';
            $this->response($this->res,'json');
        }
        $user_level = M('user_level')->where('id='.$id)->field('id,title,price,day_num,expri_day,fenyong_1,fenyong_2,jiangli_1,jiangli_2,is_red,is_r')->find();
        /**
         * 判断用户是否可以购买会员
         * 满足条件：
         * 1.会员价格收费，即price>0
         * 2.当前会员已到期
         */  
        if($user_level['price'] == 0){
            $this->res['code'] = 102;
            $this->res['msg'] = '该会员等级为免费试用，无需购买';
            $this->response($this->res,'json');
        }
        // 获取免费的会员等级id
        $level_free = M('user_level')->where(array('price'=>0))->field('id')->find();
        $level_id_free = $level_free['id'];
        
        if($this->user['level_id'] != $level_id_free){
            if(strtotime($this->user['expri_day']) > time()){
                $this->res['code'] = 103;
                $this->res['msg'] = '您当前会员等级还未到期，请勿重复购买！';
                $this->response($this->res,'json');
            }
        }
        // 获取平台设置支付方式列表
        $pay_type_list = M('pay_type')->order('o asc')->field('id,name,icon')->select();
        foreach ($pay_type_list as $k=>&$v){
            $pay_type_list[$k]['icon'] = C('URL').$v['icon'];
        }
        $this->res['msg'] = 'success';
        $this->res['data']['info'] = $user_level;
        $this->res['data']['pay_type'] = $pay_type_list;
        $this->response($this->res,'json');
    }
    
    /**
     * 确认提交订单
     */
    public function submit()
    {
        if(!IS_POST){
            $this->res['code'] = 101;
            $this->res['msg'] = '请使用POST提交数据';
            $this->response($this->res,'json');
        }
        // 获取免费的会员等级id
        $level_free = M('user_level')->where(array('price'=>0))->field('id')->find();
        $level_id_free = $level_free['id'];
        if($this->user['level_id'] != $level_id_free){
            if(strtotime($this->user['expri_day']) > time()){
                $this->res['code'] = 100;
                $this->res['msg'] = '您当前会员等级还未到期，请勿重复购买！';
                $this->response($this->res,'json');
            }
        }
        
        $post_data = file_get_contents('php://input');
        $json = json_decode($post_data);
        $level_id = $json->level_id;
        $pay_id = $json->pay_id;
		$price_actual = $json->price_actual;
        if(!$level_id or !$pay_id){
            $this->res['code'] = 102;
            $this->res['msg'] = '等级id或支付方式为空';
            $this->response($this->res,'json');
        }
        $level_info = M('user_level')->where('id='.$level_id)->find();
        
        $buy_num = M('user_level_buy')->where(array('uid'=>$this->user['id'],'level_id'=>$level_id,'status'=>array('lt',2)))->count();
        if($buy_num > 0){
            $this->res['code'] = 103;
            $this->res['msg'] = '您还有待完成支付的会员等级订单，请勿重复购买！';
            $this->response($this->res,'json');
        }
        
        $order_no = getOrderNo();
        $data = array(
            'order_no'=>$order_no,
            'money'=>round($price_actual,2),
            'uid'=>$this->user['id'],
            't'=>time(),
            'level_id'=>$level_id,
            'status'=>0,
            'pay_id'=>$pay_id
        );
        // 插入订单表
        $res = M('user_level_buy')->data($data)->add();
        if($res){
            $this->res['msg'] = 'success';
            $this->res['data'] = $order_no;
            $this->response($this->res,'json');
        }else{
            $this->res['code'] = 500;
            $this->res['msg'] = '创建订单失败，请检查表单项是否合法！';
            $this->response($this->res,'json');
        }
    }
    
    
    /**
     * 会员申请记录
     */
    public function applyPage()
    {
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $pagesize = isset($_GET['pagesize']) ? intval($_GET['pagesize']) : '10';
        $m = M('user_level_buy');
        $where['uid'] = $this->user['id'];
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $count = $m->where($where)->count();
        $list = $m->where($where)->order('t desc')->field('order_no,money,t,level_id,status')->limit($offset . ',' . $pagesize)->select();
        foreach ($list as $k=>&$v){
            // 会员等级
            $user_level = M('user_level')->where('id='.$v['level_id'])->field('title')->find();
            $list[$k]['level'] = $user_level['title'];
            $list[$k]['t'] = date('Y-m-d H:i:s',$v['t']);
        }
        $page = new \Think\Page($count, $pagesize);
        $page->show();
        $page_arr = array(
            'totalCount'=>intval($count),
            'pageSize'=>$pagesize,
            'totalPage'=>$page->totalPages,
            'currPage'=>intval($p)
        );
        $this->res['data']['page'] = $page_arr;
        $this->res['data']['list'] = $list;
        $this->response($this->res,'json');
    }
    
    /**
     * 查看订单详情
     */
    public function orderDetail()
    {
        $order_no = I('get.order_no','','htmlentities');
        if(!$order_no){
            $this->res['code'] = 101;
            $this->res['msg'] = '订单编号为空';
            $this->response($this->res,'json');
        }
        $order_info = M('user_level_buy')->where(array('order_no'=>$order_no))->find();
        if(empty($order_info)){
            $this->res['code'] = 102;
            $this->res['msg'] = '订单记录不存在';
            $this->response($this->res,'json');
        }
        // 会员等级
        $user_level = M('user_level')->where('id='.$order_info['level_id'])->field('title')->find();
        $order_info['level'] = $user_level['title'];
        // 支付方式
        $pay_type = M('pay_type')->where(array('id'=>$order_info['pay_id']))->field('name,type,account,qrcode')->find();
        $order_info['pay_name'] = $pay_type['name'];
        if($pay_type['type'] == 1){
            $order_info['account'] = $pay_type['account'];
        }else{
            $order_info['qrcode'] = C('URL').$pay_type['qrcode'];
        }
        //下单时间
        $order_info['t'] = date('Y-m-d H:i:s',$order_info['t']);
        if($order_info['status'] != 0){
            // 提交时间
            $order_info['update_t'] = date('Y-m-d H:i:s',$order_info['update_t']);
            if($order_info['pz']){
                $pz_arr = explode(',', $order_info['pz']);
                foreach ($pz_arr as $k=>&$v){
                    $pz_arr[$k] = C('URL').$v;
                }
                $order_info['pz'] = $pz_arr;
            }else{
                $order_info['pz'] = [];
            }
            
        }
        if($order_info['status'] == 2){
            // 支付时间
            $order_info['finish_t'] = date('Y-m-d H:i:s',$order_info['finish_t']);
        }
        $this->res['data'] = $order_info;
        $this->response($this->res,'json');
    }
    
    /**
     * 更改订单提交状态
     */
    public function changeOrderState()
    {
        if(!IS_POST){
            $this->res['code'] = 101;
            $this->res['msg'] = '请使用POST方法提交';
            $this->response($this->res,'json');
        }
        $post_data = file_get_contents('php://input');
        $json = json_decode($post_data);
        $pz = $json->pz;
        $remark = $json->remark;
        $order_no = $json->order_no;
        if(!$pz){
            $this->res['code'] = 102;
            $this->res['msg'] = '请上传支付凭证';
            $this->response($this->res,'json');
        }
        if(!$order_no){
            $this->res['code'] = 103;
            $this->res['msg'] = '订单号为空';
            $this->response($this->res,'json');
        }
        
        $data = array(
            'status'=>1,
            'update_t'=>time(),
            'remark'=>$remark,
            'pz'=>implode(',', $pz)
        );
        $res = M('user_level_buy')->where(array('order_no'=>$order_no))->data($data)->save();
        if($res){
            $this->res['msg'] = 'success';
            $this->response($this->res,'json');
        }else{
            $this->res['code'] = 500;
            $this->res['msg'] = '提交失败，检查您的输入项是否合法！';
            $this->response($this->res,'json');
        }
    }
}
    